package com.kx.yooli.kx.wechat.utils.captcha;

import com.baomidou.kaptcha.GoogleKaptcha;
import com.baomidou.kaptcha.Kaptcha;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.kx.yooli.kx.wechat.constant.Constants;
import com.kx.yooli.kx.wechat.constant.ErrorCodeMsg;
import com.kx.yooli.kx.wechat.po.result.AjaxResult;
import com.kx.yooli.kx.wechat.utils.redis.RedisUtil;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Base64;
import java.util.Map;

/**
 * @author john_sqwen-childe.wen
 * date 2024/4/2
 * PROJECT_NAME wechat
 * description create CaptchaImage by john_sqwen
 * and captcha value is
 */
@Component
public class CaptchaImage {
    private final Logger log = LoggerFactory.getLogger(CaptchaImage.class);
   private final Kaptcha kaptcha = new GoogleKaptcha(new DefaultKaptcha());
    @Resource
    private  RedisUtil redisUtil;
    /**
     * render captcha in request session
     * the super interface method has impl class
     */
    public void render(){
        kaptcha.render();
    }

    /**
     *
     * @param code spring boot kaptcha valid not set date
     *   default timeout 900 seconds
     */
    public void validDefaultTime(@RequestParam String code ){
     kaptcha.validate(code);
    }

    /**
     *
     * @param code spring boot kaptcha valid
     * @param second set second
     */
    public void validate(String code, long second){
        kaptcha.validate(code,second);
    }

    /**
     *
     * @return string captcha value
     */
    public AjaxResult easyGeneratorCode(HttpServletRequest request){
        // normal captcha
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 28, 5);
        specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        specCaptcha.setFont(new Font("雅黑", Font.PLAIN, 28));
        String captchaText = specCaptcha.text();
        // Encode the image to Base64
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        specCaptcha.out(outputStream);
        byte[] imageBytes = outputStream.toByteArray();
        String base64Image = Base64.getEncoder().encodeToString(imageBytes);
        String sessionID = request.getSession().getId();
        // Store in Redis
        redisUtil.remove("verifyCode::" + sessionID);
        try {
            redisUtil.setNx("verifyCode::" + sessionID, captchaText, 180);
            Map<String,String> map = Map.of("base64Image", base64Image, "captchaText", captchaText, "sid", sessionID);
            return AjaxResult.success(map);
        } catch (Exception e) {
            return AjaxResult.error(ErrorCodeMsg.NETWORK_EXCEPTION_IMG.getCode(), ErrorCodeMsg.NETWORK_EXCEPTION_IMG.getDesc());
        }
    }
}
